<?php
App::uses('AppController', 'Controller');
App::import('Helper','View/Helper/Msg');
/**
 * Seasons Controller
 *
 * @property Season $Season
 */
class SeasonsController extends AppController {
    public $components = array('RequestHandler','Session');
    public $helpers = array('Function','Msg','Js');
    public $paginate = array('limit'=>10);
    /**
     * Indexs method
     *
     * @return void
     */
    public function index() {
        $this->layout = 'index';

        /**
         * Adicinado filtro
         */
        $options = array('1'=>'Ativa','0'=>'Desativado');
        $this->Filter->addFilters(
            array(
                'anime' => array(
                    'Anime.name' => array(
                        'operator' => 'LIKE',
                        'value' => array(
                            'before' => '%', // opcional
                            'after'  => '%'  // opcional
                        )
                    )
                ),
                'ano' => array(
                    'Season.release_year' => array(
                        'operator' => '=',
                    )
                ),
                'situacao'=>array(
                    'Season.situation_id'=>array(
                        'select' => $this->Filter->select('Situacao', $this->Season->Situation->find('list')),
                    )
                ),
                't_status'=>array(
                    'Season.status'=>array(
                        'select' => $this->Filter->select('Status',$options),
                    )
                )
            )
        );

        $this->Filter->setPaginate('order', 'Season.id DESC'); // opcional
        //$this->Filter->setPaginate('limit', 10);              // opcional

        // Define conditions
        $this->Filter->setPaginate('conditions', $this->Filter->getConditions());

        $this->Season->recursive = 0;
        $seasons = $this->paginate();

        $menu_cad = 'active';
        $active_season = 'active';
        $title_for_layout = 'Temporada';
        $this->set(compact('seasons','username','menu_cad','active_season','title_for_layout'));
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $this->layout=false;
        if (!$this->Season->exists($id)) {
            throw new NotFoundException(__('Invalid season'));
        }
        $options = array('conditions' => array('Season.' . $this->Season->primaryKey => $id));
        $this->set('season', $this->Season->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        $this->layout= 'cadastros';
        $new = 1;
        if ($this->request->is('post')) {
            $this->Season->create();
            if ($this->Season->save($this->request->data)) {
                $this->Session->setFlash($this->btnClose.'<strong>Concluido:</strong> A temporada do anime foi adicionada com sucesso.','default',array('escape'=>false,'class'=>'alert alert-success'));
                $this->redirect(array('action' => 'Index'));
            } else {
                $this->Session->setFlash($this->btnClose.'<strong>Error:</strong> Houve um error inesperado ao salvar a temporada do anime.','default',array('escape'=>false,'class'=>'alert alert-error'));
            }
        }
        $animes = $this->Season->Anime->find('list',array('conditions'=>array('status'=>1)));
        $situations = $this->Season->Situation->find('list',array('conditions'=>array('status'=>1)));
        $ratings = $this->Season->Rating->find('list',array('conditions'=>array('status'=>1)));
        $this->set(compact('animes', 'situations', 'ratings','new'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->layout= 'cadastros';
        $new = '';
        if (!$this->Season->exists($id)) {
            $this->Session->setFlash($this->btnClose.'<strong>Error:</strong> Não foi possivel localizar esta temporada em nossa base de dados.','default',array('escape'=>false,'class'=>'alert alert-error'));
            $this->redirect(array('action' => 'Index'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Season->save($this->request->data)) {
                $this->Session->setFlash($this->btnClose.'<strong>Alteração:</strong> A alteração da temporada foi realizada com sucesso.','default',array('escape'=>false,'class'=>'alert alert-success'));
                $this->redirect(array('action' => 'Index'));
            } else {
                $this->Session->setFlash($this->btnClose.'<strong>Error:</strong> Houve um error inesperado ao salvar a temporada do anime.','default',array('escape'=>false,'class'=>'alert alert-error'));
            }
        } else {
            $options = array('conditions' => array('Season.' . $this->Season->primaryKey => $id));
            $this->request->data = $this->Season->find('first', $options);
        }
        $animes = $this->Season->Anime->find('list',array('conditions'=>array('status'=>1)));
        $situations = $this->Season->Situation->find('list',array('conditions'=>array('status'=>1)));
        $ratings = $this->Season->Rating->find('list',array('conditions'=>array('status'=>1)));
        $this->set(compact('animes', 'situations', 'ratings','new'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Season->id = $id;
        if (!$this->Season->exists()) {
            $this->Session->setFlash($this->btnClose.'<strong>Error:</strong> Não foi possivel localizar esta temporada em nossa base de dados.','default',array('escape'=>false,'class'=>'alert alert-error'));
            $this->redirect(array('action' => 'Index'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Season->delete()) {
            $this->Session->setFlash($this->btnClose.'<strong>Concluido:</strong> A temporada do anime foi apagada com sucesso..','default',array('escape'=>false,'class'=>'alert alert-success'));
            $this->redirect(array('action' => 'Index'));
        }
        $this->Session->setFlash($this->btnClose.'<strong>Error:</strong> Houve um error inesperado ao excluir esta temporda.','default',array('escape'=>false,'class'=>'alert alert-success'));
        $this->redirect(array('action' => 'Index'));
    }

    public function status($id=null,$status=null)
    {
        $this->autoRender = false;
        $this->Season->id = $id;
        if(!$this->Season->exists($id))
        {
            $this->Session->setFlash('error');
            return null;
        }
        if(!empty($status) || $status == 0)
        {
            if($this->Season->saveField('status',$status))
            {
                if($status == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return null;
            }
        }else{
            return null;
        }
    }
}
